#!/bin/sh

set -e

#For 20GB
#SNAP_SIZE="-L20G"
SNAP_SIZE="-l50%FREE"
SNAP_MOUNTPOINT="$2"

CDIR=`dirname $0`
. $CDIR/filesystem_snapshot_common
exit_exclude_snapshot_mountpoints "$SNAP_MOUNTPOINT"

mkdir -p /mnt/urbackup_snaps

SNAP_ID=$1
SNAP_DEST=/mnt/urbackup_snaps/$SNAP_ID

if lsblk -r --output "NAME,MOUNTPOINT" --paths > /dev/null 2>&1
then
	VOLNAME=`lsblk -r --output "NAME,MOUNTPOINT" --paths | egrep " ${SNAP_MOUNTPOINT}\$" | head -n 1 | tr -s " " | cut -d" " -f1`
else
	VOLNAME=`lsblk -r --output "NAME,MOUNTPOINT" | egrep " ${SNAP_MOUNTPOINT}\$" | head -n 1 | tr -s " " | cut -d" " -f1`
	VOLNAME="/dev/mapper/$VOLNAME"
fi

set_filesystem_type "${SNAP_MOUNTPOINT}"

if [ "x$TYPE" = "x" ]
then
        if btrfs subvolume list -o "$SNAP_MOUNTPOINT" > /dev/null 2>&1
        then
                TYPE="btrfs"
        fi
fi

if [ "x$TYPE" = "xbtrfs" ]
then
    $CDIR/btrfs_create_filesystem_snapshot "$@"
    exit $?
fi

if [ "x$VOLNAME" = x ]
then
    echo "Could not find LVM volume for mountpoint ${SNAP_MOUNTPOINT}"
    exit 1
fi

export LVM_SUPPRESS_FD_WARNINGS=1

VGNAME=`lvdisplay "$VOLNAME" | grep "VG Name" | tr -s " " | cut -d" " -f4`
if [ "x$VGNAME" = x ]
then
	echo "Could not find LVM volume group of volume $VOLNAME"
    exit 1
fi

if [ "x$SNAP_ID" = x ]
then
    echo "No snapshot id specified"
    exit 1
fi

lvcreate $SNAP_SIZE -s -n urbackup_snap_$SNAP_ID "$VOLNAME"

mkdir -p /mnt/urbackup_snaps/$SNAP_ID

MOUNTOPTS="ro"

if [ $TYPE = "xfs" ]
then
    MOUNTOPTS="ro,nouuid"
fi

if ! mount -o $MOUNTOPTS /dev/$VGNAME/urbackup_snap_$SNAP_ID /mnt/urbackup_snaps/$SNAP_ID
then
	echo "Mounting filesystem failed"
	rmdir /mnt/urbackup_snaps/$SNAP_ID
	lvremove -f /dev/$VGNAME/urbackup_snap_$SNAP_ID
	exit 1
fi

echo "SNAPSHOT=/mnt/urbackup_snaps/$SNAP_ID"

exit 0